package debuggen;

import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;

/**
 * Reader
 * Simple class that offers the ability to read the debug log file.
 * @author Danny
 *
 */
public class Reader extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6405758073295918042L;
	// Path to debug file
	private String strDebugFilePath;
	// Document 
	private Document doc;
	// Document builder
	private DocumentBuilder builder;
	// File xml
	private File flXML;
	// JTable
	private JTable tafel;
	// List with columns names
	private String[] 		arrColumnNames = {"Date", 
			  								  "Sequence", 
			  								  "Level", 
			  								  "Thread", 
			  								  "Message"};
	
	/**
	 * Constructor
	 */
	public Reader()
	{
		config();
	}
	/**
	 * Config
	 * Set some basic config
	 */
	private void config()
	{
		/**
		 * Some basic info
		 */
		strDebugFilePath = System.getProperty("user.dir") + "/debug.log";

		
		/**
		 * init xml parser
		 */
		initXML();
		
		/**
		 * init GUI
		 */
		initGUI();
		
	}
	/**
	 * initGUI
	 */
	private void initGUI()
	{
		// set size
		setSize(500, 500);
		// Set title
		setTitle("Debug viewer IDP project");
		// Set resizable to false!
		setResizable(false);
		
		/**
		 * Create table
		 */
		tafel = new JTable();
		// Set size with scrolls
		tafel.setPreferredScrollableViewportSize(this.getContentPane().getSize());
		tafel.setFillsViewportHeight(true);
		
		JScrollPane scrollPane = new JScrollPane(tafel);
		
		add(scrollPane);
		
		// Make everything visible
		setVisible(true);
		pack();
		
		
	}
	/**
	 * initXML
	 */
	private void initXML()
	{
		try 
		{
			// Make builder for parser
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			// Make file
			flXML = new File(strDebugFilePath);
			// Create document
			doc = builder.parse(flXML);
			
			NodeList nl = doc.getDocumentElement().getChildNodes();
			
			for(int i = 0; i < nl.getLength(); i++){
				Node n = nl.item(i);
				System.out.println("Node: " + n.getNodeName() + " value: " + n.getNodeValue());
			}
		}catch(Exception exc)
		{
			exc.printStackTrace();
		}
	}
	 private static String getTagValue(String sTag, Element eElement){
		    NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
		    Node nValue = (Node) nlList.item(0); 
		 
		    return nValue.getNodeValue();    
		 }

	// TESTER
	public static void main(String[] args)
	{
		new Reader();
	}
}
